package b3.widget.store
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.PopMessage;
	import b3.widget.tab.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;

	public class StoreItem extends Sprite
	{
		private var objectIndex:int;

		public function StoreItem(objectIndex:int)
		{
			super();
			this.objectIndex = objectIndex; 
			var objectConfig:ObjConfig = Config.objConfigs[objectIndex];
			//背景框
			var StoreItemBgr:Class = ResourceHelper.getInstance().getClassByName("StoreItemBgr");
			var tabItemBackground:Sprite = new StoreItemBgr();
			addChild(tabItemBackground);
			//标题名字
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.text = objectConfig.name;
			if (titleText.text.length > 6)
			{
				titleText.setTextFormat(new TextFormat("微软雅黑", 14, 0x000000, true));
				titleText.y = 8;
			}
			else
			{
				titleText.setTextFormat(new TextFormat("微软雅黑", 15, 0x000000, true));
				titleText.y = 6;
			}
			titleText.x = (this.width - titleText.width) / 2;
			//物品图片
			var ObjectImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage[0]);
			var objectSprite:Sprite = new ObjectImage();
			objectSprite.width = 65;
			objectSprite.scaleY = objectSprite.scaleX;
			objectSprite.x = 13;
			objectSprite.y = 35;
			addChild(objectSprite);
			addChild(titleText);
			
			var priceIconStr:String;
			var price:int;
			if (objectConfig.priceGoldIngot > 0)
			{
				priceIconStr = "PriceQDian";
				price = objectConfig.priceGoldIngot; 
			}
			else if (objectConfig.priceGoldCoin > 0)
			{
				priceIconStr = "GoldCoinIcon";
				price = objectConfig.priceGoldCoin; 
			}
			else if (objectConfig.priceLight > 0)
			{
				priceIconStr = "LightIcon";
				price = objectConfig.priceLight; 
			}
			else
			{
				priceIconStr = "DarkIcon";
				price = objectConfig.priceDark; 
			}
			//非卖品
			if (objectConfig.notOnSale)
			{
				//价格
				var notOnSaleText:TextField = new TextField();
				notOnSaleText.selectable = false;
				notOnSaleText.autoSize = TextFieldAutoSize.LEFT;
				notOnSaleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
				notOnSaleText.text = "绝版商品";
				notOnSaleText.x = 115 - notOnSaleText.width / 2;
				notOnSaleText.y = 55;
				addChild(notOnSaleText);
			}
			//特卖物品
			else if (objectConfig.discount > 0)
			{
				var priceSprite:Sprite = Store.getPriceSprite("原价:", price, priceIconStr, 0xFFFFFF);
				priceSprite.x = 125 - priceSprite.width / 2;
				priceSprite.y = 25;
				var lineSprite:Sprite = new Sprite();
				lineSprite.graphics.lineStyle(2, 0xFF0000);
				lineSprite.graphics.moveTo(-5, priceSprite.height - 5);
				lineSprite.graphics.lineTo(priceSprite.width, 5);
				priceSprite.addChild(lineSprite);
				addChild(priceSprite);
				var DiscountLock:Class = ResourceHelper.getInstance().getClassByName("StoreItemLock3");
				var discountLock:Sprite = new DiscountLock();
				discountLock.x = 0;
				discountLock.y = -10;
				discountLock.width = 90;
				discountLock.height = 70;
				addChild(discountLock);
				priceSprite = Store.getPriceSprite("特卖价:", price * objectConfig.discount / 100, priceIconStr, 0xFFFFFF);
				priceSprite.x = 125 - priceSprite.width / 2;
				priceSprite.y = 62;
				addChild(priceSprite);
			}
			//Q点直购
			else if (objectConfig.priceGoldIngot > 0)
			{
				priceSprite = Store.getPriceSprite("价格:", price, priceIconStr, 0xFFFFFF);
				priceSprite.x = 125 - priceSprite.width / 2;
				priceSprite.y = 25;
				addChild(priceSprite);
				priceSprite = Store.getPriceSprite("黄钻价格:", price * 0.8, priceIconStr, 0xFFFFFF);
				priceSprite.x = 125 - priceSprite.width / 2;
				priceSprite.y = 62;
				addChild(priceSprite);
			}
			else
			{
				priceSprite = Store.getPriceSprite("价格:", price, priceIconStr, 0xFFFFFF);
				priceSprite.x = 115 - priceSprite.width / 2;
				priceSprite.y = 35;
				addChild(priceSprite);
			}

			this.mouseChildren = false;
			addEventListener(MouseEvent.CLICK, onMouseClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var objectConfig:ObjConfig = Config.objConfigs[objectIndex];
			var me:Player = PlayerManager.getInstance().me;
			//非卖品
			if (objectConfig.notOnSale)
			{
				var popMessage:PopMessage = new PopMessage("非卖品展示，无法购买！", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage]);
			}
			//Q点直购
			else if (objectConfig.priceGoldIngot > 0)
			{
				var price:int = objectConfig.discount > 0 ? objectConfig.priceGoldIngot * objectConfig.discount / 100 : objectConfig.priceGoldIngot;
				var payitem:String = objectConfig.index + "*" + price + "*1";
				var goodsmeta:String = objectConfig.name + "*" + objectConfig.description;
				var goodsurl:String = Profile.URL_DevelopPage + "pic/" + objectConfig.iconImage + ".jpg";
        		me.packageModel.buyGoods(payitem, goodsmeta, goodsurl);
			}
			else
			{
				var purchaseDialog:PurchaseDialog = new PurchaseDialog(objectIndex, 0);
				WidgetManager.getInstance().popOut([purchaseDialog], true);
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
			var itemTip:StoreItemMessage = new StoreItemMessage(objectIndex); 
			itemTip.x = globalPoint.x + 166.9;
			itemTip.y = globalPoint.y;
			if (itemTip.x + itemTip.width > BFont.stageWidth)
				itemTip.x = globalPoint.x - itemTip.width;
			if (itemTip.y + itemTip.height > BFont.stageHeight)
				itemTip.y = BFont.stageHeight - itemTip.height;
			WidgetManager.getInstance().popOutMouseOverMessage(itemTip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			WidgetManager.getInstance().closeMouseOverMessage();
		}
		
	}
}